package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.mapper.StrategyMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private StrategyMapper strategyMapper;

    public PageInfo<?> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo<>(strategyMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(Strategy entity) {
        if (entity.getId()!=null){
            strategyMapper.updateByPrimaryKey(entity);
        }else {
            strategyMapper.insert(entity);
        }
    }

    @Override
    public List<Strategy> listAll() {
        return strategyMapper.selectAll();
    }

    @Override
    public Strategy getById(Long id) {
        return strategyMapper.selectByPrimaryKey(id);
    }

    @Override
    public Strategy selectStrategyByKeyWord(QueryObject qo) {
        Strategy strategy = strategyMapper.selectStrategyByKeyWord(qo);
        if (strategy == null){
            throw new RuntimeException("无数据");
        }
        return strategy;
    }

    @Override
    public Integer queryLikeCount(Long strategyId) {
        return strategyMapper.selectLikeCount(strategyId);
    }

    @Override
    public void updateLike(int likeCount, Long strategyId) {
        strategyMapper.updateLikeCount(likeCount,strategyId);
    }

    @Override
    public Integer queryCollectCount(Long strategyId) {
        return strategyMapper.selectCollectCount(strategyId);
    }

    @Override
    public void updateCollectCount(int collectCount, Long strategyId) {
        strategyMapper.updateCoolectCount(collectCount,strategyId);
    }

    //主页搜索
    @Override
    public PageInfo<?> selectListByKeyword(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        return new PageInfo<>(strategyMapper.selectForList(qo));
    }
}
